package com.wu.zr

import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.wu.common.util.TimerUtil
import com.wu.zr.databinding.ActivityMainBinding
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.schedule


class MainActivity : AppCompatActivity() {

    var img = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg11.51tietu.net%2Fpic%2F2016-071418%2F20160714181543xyu10ukncwf221991.jpg&refer=http%3A%2F%2Fimg11.51tietu.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1631771317&t=b8bfe3f8f419bf76caee4db172637917"
    var img2 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwx3.sinaimg.cn%2Flarge%2F547f46d9ly4gsqmhfbuf1j20u00g2whd.jpg&refer=http%3A%2F%2Fwx3.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1631771317&t=7678a931783ccb04a45c033574869521"
    var img3 = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn1-q.mafengwo.net%2Fs16%2FM00%2F79%2F04%2FCoUBUl_hnDaAb8vsAAEeWevbnhw68.jpeg&refer=http%3A%2F%2Fn1-q.mafengwo.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1631771317&t=9ada084c10cb4900b3cfc15f6e824963"
    var binding: ActivityMainBinding? = null

    //倒计时
    lateinit var timer: Timer

    //位置
    var count = 0;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        initView()
        setData()
//        binding!!.ivForeground.setImageResource(R.drawable.foreground1)
    }

    private fun setData() {
        var imgs = ArrayList<String>()
        imgs.add(img)
        imgs.add(img2)
        imgs.add(img3)
        var mAdapter = ZrBgAdapter(this);
        binding!!.vpBg.adapter = mAdapter
        mAdapter.addItems(imgs)
    }

    fun initView() {
        transformer()
        setTimer()
    }



    //设计自动播放 和无限播放
    private fun setTimer() {

        var timerUtil= TimerUtil()

        timerUtil.intervalTime=2000
        timerUtil.setTimerLiener(object :TimerUtil.TimeListener{
            override fun onFinish() {
                timerUtil.release()
            }
            override fun onInterval(var1: Long) {
                MyPagerHelper.setCurrentItem(binding!!.vpBg, count++, 300)
            }
        })
        timerUtil.startTimer()
        binding!!.vpBg.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            }

            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                count = position
            }
        })


    }

    //设置切换动画
    private fun transformer(
    ) {
        //设置多个果冻动画 CompositePageTransformer
        val compositePageTransformer = CompositePageTransformer()
        // 叠加 渐变
        compositePageTransformer.addTransformer(ViewPagerOverlay())
        //偏移
        //        compositePageTransformer.addTransformer(MarginPageTransformer(100))
        binding!!.vpBg.setPageTransformer(compositePageTransformer)

    }

}